<?php
class Application_Model_ContentMapper extends Application_Model_Mapper
{
  protected $_dbTableClass = "Application_Model_DbTable_Content";
  protected $_model = "Application_Model_Content";

  public function save(Application_Model_Base $obj)
  {
    $pk = $obj->getPrimaryKey();
    $options = $obj->getOptions();
    unset($options['upload_time']);
    if (!($id = $obj->get($pk))) {
      return $this->getDbTable()->insert($options);
    } 
    else {
      $data = $options;
      unset($data[$pk]);
      return $this->getDbTable()->update($data, "$pk = $id");
    }
  }
  static public function video_duration($file) 
  {
	  $info = '';
	  $fp = popen(FFMPEG." -i $file 2>&1 | gawk  '/Duration/{print $0}'", "r");
	  while($c = fgets( $fp ) ) 
	  {
		  $info .= $c;
	  }
	  pclose($fp);
	  // 通过使用输出缓冲，获取到ffmpeg所有输出的内容。
	  $ret = array();
	  // Duration: 01:24:12.73, start: 0.000000, bitrate: 456 kb/s
	  if (preg_match("/Duration: (.*?),.*/", $info, $match)) {
		  $duration = explode(".", $match[1]);// 提取出播放时间
		  $ret['duration'] = $duration[0]; 
		  $da = explode(':', $match[1]); 
		  $ret['seconds'] = $da[0] * 3600 + $da[1] * 60 + $da[2]; // 转换为秒
		  return $ret;
	  }
	  return null;
  }
}
